<?=$this->headTitle('Редактиование профайла');?>

<script type="text/javascript">
$(function(){
    $('body').data('check',false);
    var data = {};

    $('form#userprofile').find('#submit').hover(function(){
        $(this).focus();
    });

    $('form#userprofile').find('input[type=text]').blur(function(){
        data[$(this).attr('id')] = $(this).val();
        send(data);
    });

    $('input[name=image]').change(function(){
    var userform = $('form#userprofile');
        userform.ajaxStart(function() {

        });
        userform.ajaxComplete(function() {

        });
        userform.append(createIFrame('iFrame'));
        userform.attr({'action':'/user/avatarupload','target':'iFrame'}).removeAttr('onsubmit');;
        userform.find('#submit').click();

        userform.attr({'action':'/user/editprofile','onsubmit':'return checkform()'}).removeAttr('target');
    });

});

function send(data){

    var errors=[],inp,inpWidth,positionEmail,positionLogin,positionName,max,form,childNodes,ok=[];
    positionEmail = $('input#email').position();
    positionLogin = $('input#userlogin').position();
    positionName= $('input#username').position();
    inpWidth = $('input#email').width()/1.1;

    $.ajax({
        url:'/users/user-login/input-validate/format/json',
        data:data,
        type:'post',
        dataType:'json',
        success:function(resp){
             form = document.getElementById('userprofile');
             childNodes = form.getElementsByTagName('input');
            var inpEmail = $('input#email');
            var inpLogin = $('input#userlogin');
            var inpName = $('input#username');
            $('body').data('check',false);
            if(resp.email){

                $('body').data('check',false);
                var email = [];


                var popups = inpEmail.parent().find('.bottom');

                if(popups.length > 0){popups.remove();}

                for(var e in resp.email){
                    email.push('<li>'+resp.email[e]+'</li>');
                }
                var styleE = "top:"+(positionEmail.top+40)+"px;left:"+(positionEmail.left+inpWidth)+"px";
                var popupE = '<div class="popup-alert bottom" style='+styleE+'>'+email+'</div>';
                inpEmail.parent().append($(popupE).fadeIn('fast'));
                var alertDivE = inpEmail.parent().find('.popup-alert bottom');
                setTimeout(function(){alertDivE.remove();},5000);
            }else{

                inpEmail.parent().find('.bottom').remove();
            }
             if(resp.userlogin){
                 $('body').data('check',false);
                 var login = [];

                 var popupsL = inpLogin.parent().find('.top');

                 if(popupsL.length > 0){popupsL.remove();}

                 for(var u in resp.userlogin){
                     login.push('<li>'+resp.userlogin[u]+'</li>');
                 }
                 var styleL = "top:"+(positionLogin.top-75)+"px;left:"+(positionLogin.left+inpWidth)+"px";
                 var popupL = '<div class="popup-alert top" style='+styleL+'>'+login+'</div>';
                 inpLogin.parent().append($(popupL).fadeIn('fast'));
                 var alertDivL = inpLogin.parent().find('.popup-alert top');
                 setTimeout(function(){alertDivL.remove();},5000);
            }else{

                 inpLogin.parent().find('.top').remove();
             }
            if(resp.username){
                $('body').data('check',false);
                var name = [];

                var popupsN = inpName.parent().find('.right');

                if(popupsN.length > 0){popupsN.remove();}

                for(var n in resp.username){
                    name.push('<li>'+resp.username[n]+'</li>');
                }
                var styleN = "top:"+(positionName.top-20)+"px;left:"+(positionName.left+inpWidth+50)+"px";
                var popupN = '<div class="popup-alert right" style='+styleN+'>'+name+'</div>';
                inpName.parent().append($(popupN).fadeIn('fast'));
                var alertDivN = inpName.parent().find('.popup-alert right');
                setTimeout(function(){alertDivN.remove();},5000);
            }else{

                inpName.parent().find('.right').remove();
            }
          if(resp.email || resp.userlogin || resp.username){
              $('body').data('check',false);
          }else{
              $('body').data('check',true);
          }
        }
    });
    return $('body').data('check');
}
function checkform(){
    var data = {},result,elements,max,i=0;

    elements = $('form#userprofile').find('input[type=text]');
    max =  elements.length;
    for(i;i < max;i+=1){
        data[$(elements[i]).attr('id')] = $(elements[i]).val();
    }
    result = send(data);

    return result;
}
function createIFrame(fname){

    var frame = $('<iframe>',{name:fname,width:0,height:0}).css('display','none');
    $('form#userprofile').append(frame);
}

</script>
<div id="img"></div>
<?=$this->profile;?>
